<template>
  <div>
    <div class="main">
      <el-button type="primary" size="medium" @click="dialog_add = true"
        >发布新公告<i class="el-icon-upload el-icon--right"></i
      ></el-button>
      <el-table :data="allData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column label="内容">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>公告内容: {{ scope.row.titleContents }}</p>
              <div slot="reference" class="name-wrapper">
                <span size="medium">{{ scope.row.titleContents }}</span>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="日期" prop="createTime" width="180">
        </el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <span
              class="success"
              :class="
                scope.row.flag == '0'
                  ? 'delete'
                  : scope.row.flag == '1'
                  ? 'success'
                  : 'error'
              "
              >{{
                scope.row.flag == "0"
                  ? "已删除"
                  : scope.row.flag == "1"
                  ? "正在播出"
                  : "待播出"
              }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button
              type="success"
              plain
              v-if="scope.row.flag != '1'"
              size="mini"
              @click="playThis(scope.row)"
              >播出</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="pageNo"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="发布新公告" :visible.sync="dialog_add" width="30%">
        <el-form ref="form" :model="addFrom" label-width="80px">
          <el-form-item label="公告内容">
            <el-input
              type="textarea"
              v-model="addFrom.titleContents"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button size="small" type="primary" @click="addSubmit"
              >立即创建</el-button
            >
            <el-button size="small" @click="dialog_add = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="编辑公告" :visible.sync="dialog_updata" width="30%">
        <el-form ref="form" :model="updataFrom" label-width="80px">
          <el-form-item label="公告内容">
            <el-input
              type="textarea"
              v-model="updataFrom.titleContents"
            ></el-input>
          </el-form-item>
          <el-form-item label="公告状态">
            <el-col :span="22">
              <el-select
                v-model="updataFrom.flag"
                clearable
                class="selectWidth"
                placeholder="状态"
              >
                <!-- <el-option label="已删除" value="0"></el-option> -->
                <el-option label="正在播出" value="1"></el-option>
                <el-option label="待播出" value="2"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="updataSubmit"
              >修改</el-button
            >
            <el-button size="small" @click="dialog_updata = false"
              >取消</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getAllTitle,
  saveOrUpdateTitle,
  removeTitle,
  playThisTitle,
} from "@/api/apis";
export default {
  components: {},
  name: "index",
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      allData: [],
      dialog_add: false,
      dialog_updata: false,
      addFrom: {
        flag: "2",
        titleContents: "",
      },
      updataFrom: {},
    };
  },

  created() {
    this.getAll();
  },
  methods: {
  
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageNo = val
      this.getAll();
    },
    playThis(row) {
      playThisTitle(row.id).then((res) => {
        if (res.code === 20000) {
          this.$message({
            type: "success",
            message: "播出成功!",
          });
          this.getAll();
        } else {
          this.$message({
            type: "error",
            message: "播出失败!",
          });
        }
      });
    },
    addSubmit() {
      saveOrUpdateTitle(this.addFrom).then((res) => {
        console.log("d", res);
        if (res.code === 20000) {
          this.$message({
            type: "success",
            message: "新增成功!",
          });
          this.getAll();
        } else {
          this.$message({
            type: "error",
            message: "新增失败!",
          });
        }
        this.dialog_add = false;
      });
    },
    updataSubmit() {
      saveOrUpdateTitle(this.updataFrom).then(
        (res) => {
          // console.log("d", res);
          this.getAll(); 
          this.dialog_updata = false;
          if (res.code === 20000) {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
          } 
         
        }
      );
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialog_updata = true;
      this.updataFrom = row;
    },
    handleDelete(index, row) {
      console.log(index, row.id);
      this.$confirm("确定删除？")
        .then((_) => {
          // done();
          removeTitle(row.id).then((res) => {
            console.log(res);
            if (res.code === 20000) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getAll();
            } else {
              this.$message({
                type: "error",
                message: "删除失败!",
              });
            }
          });
        })
        .catch((_) => {});
    },
    //获取列表
    getAll() {
      getAllTitle(this.pageNo, this.pageSize).then((res) => {
        this.allData = res.data.records;
        this.total = res.data.total;
        // console.log("all", this.allData);
      });
    },
  },
};
</script>
<style lang="less" scoped>
.success {
  color: #67c23a;
  font-size: 13px;
}
.error {
  color: #e6a23c;
  font-size: 13px;
}

.delete {
  color: #e6643c;
  font-size: 13px;
}
.main {
  width: 100%;
  height: calc(100vh - 120px);
  overflow-y: auto;
  // background-color: #efefef;
}
</style>
